<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Card\Controller;
use Card\Controller\PublicController;

/**
 * 展示
 */
class ShowController extends PublicController {

    /**
     * 生成名片图片
     */
    public function CreateCardPictrue(){
        $data = inputJson();

        //假数据
        // $data['phpsid'] = 'dugah827g0a0h3a99lia4aq42s';//session_id
        // $data['card_id'] = 8;
        //图片高度宽度
        $height = 540;
        $width = 900;

        //云名片信息
        $card_id = $this->getCardId($data['phpsid']);//名片id
        if($card_id === -1){
            abnormalOutputJson(['info'=>'请先登录','type'=>1]);
        }elseif ($card_id === false) {
            errorOutputJson('i'.__LINE__);
        }elseif ($card_id === []) {
            //没有名片
            abnormalOutputJson(['info'=>'请先创建名片','type'=>2]);
        }
        $res = D('User')->find($card_id);
     //    if(empty($res['card_code'])){
     //        //没有名片二维码
     //        $path = $this->qrurl($card_id);
     //        if($path === -1){//生成云名片二维码 返回路径
     //            errorOutputJson('i'.__LINE__);
     //        }
     //        $code_data['id'] = $card_id;//名片id
     //        $code_data['card_code'] = $path;
     //        $rres = D('User')->save($code_data);
     //        if($rres === false){
     //            errorOutputJson('i'.__LINE__);
     //        }
     //        $res['card_code'] = 'http://'.$_SERVER['HTTP_HOST'].$path;//二维码路径
     //    }else{
	    //     $res['card_code'] = 'http://'.$_SERVER['HTTP_HOST'].$res['card_code'];
	    // }
        $res['card_code'] = $this->qrurlCardPic($data['card_id']);

        $name = $res['name'];// 姓名 爱文爱文  艾小雯
        $mobile = $res['moblie_phone'];// 电话

        if(empty($res['head_img'])){
            $head_img_path = C('CARD_DOMAIN')."/Uploads/Qcode/defalut_heade.png";//默认头像路径
        }else{
            $dres = D('Picture')->ImgInfo($res['head_img']);
            $head_img_path = $dres['path'];//头像路径
        }


        //如果有公司名称和公司地址，则生成第二套模板，否则第一套模板
        if(!empty($res['company']) && !empty($res['address'])){
            //创建画布(默认黑色)
            $im=imagecreatetruecolor($width,$height);
            //修改画布为黄色
            $yellow=imagecolorallocate($im,255,255,0);  
            imagefill($im,0,0,$yellow); 
            //修改画布颜色为透明
            // imagecolortransparent($im,$yellow);
            
            //贴头像
            //头像图片类型
            $pos = strrpos($head_img_path,'.');  
            $img_type = substr($head_img_path,$pos+1); 
            if($img_type == 'png' || $img_type == 'PNG'){
                $headImage=imagecreatefrompng($head_img_path);
            }else{
                $headImage=imagecreatefromjpeg($head_img_path);
            }
            //头像原图宽度
            $headImage_width = imagesx($headImage);
            //头像原图高度
            $headImage_height = imagesy($headImage);
            //头像放置的x坐标
            $x = 100;
            //头像放置的y坐标
            $y = 82;
            //头像缩放后宽度
            $dst_w = 228;
            //头像缩放后高度
            $dst_y = 228;
            imagecopyresampled($im, $headImage, $x, $y, 0, 0, $dst_w, $dst_y, $headImage_width, $headImage_height);

            //贴二维码
            $qcode=imagecreatefrompng($res['card_code']);
            //二维码原图宽度
            $qcode_width = imagesx($qcode);
            //二维码原图高度
            $qcode_height = imagesy($qcode);
            //二维码放置的x坐标
            $x = 700;
            //二维码放置的y坐标
            $y = 355;
            //二维码缩放后宽度
            $dst_w = 150;
            //二维码缩放后高度
            $dst_y = 150;
            imagecopyresampled($im, $qcode, $x, $y, 0, 0, $dst_w, $dst_y, $qcode_width, $qcode_height);

            //贴第二套模板(紫色上层图)
            $srcImage=imagecreatefrompng(C('CARD_DOMAIN')."/Uploads/CardImgs/default1.png"); 
            //紫色图原图宽度
            $srcImage_width = imagesx($srcImage);
            //紫色图原图高度
            $srcImage_height = imagesy($srcImage);
            //紫色图放置的x坐标
            $x = 0;
            //紫色图放置的y坐标
            $y = 0;
            //紫色图缩放后宽度
            $dst_w = 900;
            //紫色图缩放后高度
            $dst_y = 540;
            imagecopyresampled($im, $srcImage, $x, $y, 0, 0, $dst_w, $dst_y, $srcImage_width, $srcImage_height);

            //贴文字
            $pfont = "./Uploads/Ttf/phone.otf";//电话号码字体
            $wfont = "./Uploads/Ttf/name.TTF";//文字字体
            $white=imagecolorallocate($im,255,255,255);  
            // 文字相对于图片所在的高度
            $mobile_HeightOffset = 50;//电话
            $name_HeightOffset = 342;//姓名
            $company_HeightOffset = 216;//公司名称
            $position_HeightOffset = 450;//职位
            //如果公司名称长度小于16个字，则第一个字的高度居中，否则排第一行
            $res['address'] = $res['areaid_1'].$res['areaid_2'].$res['areaid_3'].$res['address'];
            $company_len = abslength($res['address']);//字符串长度
            if($company_len <=16){
                $address_HeightOffset = 288;//地址
                $fourth = urldecode($res['address']);//公司名称
                $address_word = utf8_substr($fourth,0,1);//地址第一个字
                $width_address_word = imagettfbbox(42, 0, $wfont, $address_word);
                $x_address_word = ceil((900 - $width_address_word[2])/4+300);
                imagettftext($im, 18, 0, $x_address_word, $address_HeightOffset, $white, $wfont, $address_word);
                $address_word = utf8_substr($fourth,1,15);//地址第一个字
                $x_address_word = ceil((900 - $width_address_word[2])/4+324);
                imagettftext($im, 18, 0, $x_address_word, $address_HeightOffset, $white, $wfont, $address_word);

            }else{
                $address_HeightOffset = 268;//地址
                $fourth = urldecode($res['address']);//公司名称
                $address_word = utf8_substr($fourth,0,1);//地址第一个字
                $width_address_word = imagettfbbox(42, 0, $wfont, $address_word);
                $x_address_word = ceil((900 - $width_address_word[2])/4+300);
                imagettftext($im, 18, 0, $x_address_word, $address_HeightOffset, $white, $wfont, $address_word);
                $address_word = utf8_substr($fourth,1,15);//地址第一个字
                $x_address_word = ceil((900 - $width_address_word[2])/4+324);
                imagettftext($im, 18, 0, $x_address_word, $address_HeightOffset, $white, $wfont, $address_word);
                //第二行第一个字
                $address_word = utf8_substr($fourth,16,1);//地址第一个字
                $width_address_word = imagettfbbox(42, 0, $wfont, $address_word);
                $x_address_word = ceil((900 - $width_address_word[2])/4+300);
                imagettftext($im, 18, 0, $x_address_word, $address_HeightOffset+30, $white, $wfont, $address_word);
                //第二行剩下的字
                $address_word = utf8_substr($fourth,17,15);//地址第一个字
                $x_address_word = ceil((900 - $width_address_word[2])/4+324);
                imagettftext($im, 18, 0, $x_address_word, $address_HeightOffset+30, $white, $wfont, $address_word);
            } 
            //姓名
             

            // 解码main.php里img标签已编码的URL字符串，包括first, last以及location
            $first = urldecode($name);//电话
            $second = urldecode($mobile);//姓名
            $third = urldecode($res['company']);//公司名称
            $five = urldecode($res['position']);//职位
            /*
            * 取得需要向图片上添加的文本的范围。imagettfbbox() 返回一个含有8个
            * 单元的数组表示了文本外框的四个角。
            */
            // $len = abslength($content);//字符串长度
            $company_first_word = utf8_substr($third,0,1);//公司名称第一个字
            $company_last_word = utf8_substr($third,1,30);//公司名称除第一个字
            $position_first_word = utf8_substr($five,0,1);//职位第一个字
            $position_last_word = utf8_substr($five,1,10);//职位除第一个字

            $width_first = imagettfbbox(68, 0, $wfont, $first);
            $width_second = imagettfbbox(42, 0, $pfont, $second);
            $width_company_first_word = imagettfbbox(42, 0, $wfont, $company_first_word);//公司第一个字
            $width_position_first_word = imagettfbbox(42, 0, $wfont, $position_first_word);//职位第一个字
            $width_five = imagettfbbox(42, 0, $wfont, $five);//职位

            // 进一法取整, $width_变量[2]代表右下角X坐标位置
            $x_first = ceil((900 - $width_first[2])/5-10);
            $x_second = ceil((900 - $width_second[2])/2+209);
            $x_third = ceil((900 - $width_third[2])/4+450);
            $x_company_first_word = ceil((900 - $width_company_first_word[2])/4+300);
            $x_company_last_word = ceil((900 - $width_company_first_word[2])/4+324);
            $x_position_first_word = ceil((900 - $width_position_first_word[2])/2-284);
            $x_position_last_word = ceil((900 - $width_position_first_word[2])/2-260);
            $x_five = ceil((900 - $width_five[2])/5+30);


            imagettftext($im, 50, 0, $x_first, $name_HeightOffset+58, $white, $wfont, $first);
            imagettftext($im, 33, 0, $x_second, $mobile_HeightOffset+90, $white, $pfont, $second);
            imagettftext($im, 18, 0, $x_company_first_word, $company_HeightOffset, $white, $wfont, $company_first_word);
            imagettftext($im, 18, 0, $x_company_last_word, $company_HeightOffset, $white, $wfont, $company_last_word);
            // imagettftext($im, 21, 0, $x_position_first_word, $position_HeightOffset, $white, $wfont, $position_first_word);
            // imagettftext($im, 21, 0, $x_position_last_word, $position_HeightOffset, $white, $wfont, $position_last_word);
            imagettftext($im, 22, 0, $x_five, $position_HeightOffset, $white, $wfont, $five);

        }elseif(!empty($res['company'])){//第三套模板

            //创建画布(默认黑色)
            $im=imagecreatetruecolor($width,$height);
            //修改画布为黄色
            $yellow=imagecolorallocate($im,255,255,0);  
            imagefill($im,0,0,$yellow); 
            //修改画布颜色为透明
            // imagecolortransparent($im,$yellow);
            
            //贴头像
            //头像图片类型
            $pos = strrpos($head_img_path,'.');  
            $img_type = substr($head_img_path,$pos+1); 
            if($img_type == 'png' || $img_type == 'PNG'){
                $headImage=imagecreatefrompng($head_img_path);
            }else{
                $headImage=imagecreatefromjpeg($head_img_path);
            }
            //头像原图宽度
            $headImage_width = imagesx($headImage);
            //头像原图高度
            $headImage_height = imagesy($headImage);
            //头像放置的x坐标
            $x = 100;
            //头像放置的y坐标
            $y = 82;
            //头像缩放后宽度
            $dst_w = 228;
            //头像缩放后高度
            $dst_y = 228;
            imagecopyresampled($im, $headImage, $x, $y, 0, 0, $dst_w, $dst_y, $headImage_width, $headImage_height);

            //贴二维码
            $qcode=imagecreatefrompng($res['card_code']);
            //二维码原图宽度
            $qcode_width = imagesx($qcode);
            //二维码原图高度
            $qcode_height = imagesy($qcode);
            //二维码放置的x坐标
            $x = 700;
            //二维码放置的y坐标
            $y = 355;
            //二维码缩放后宽度
            $dst_w = 150;
            //二维码缩放后高度
            $dst_y = 150;
            imagecopyresampled($im, $qcode, $x, $y, 0, 0, $dst_w, $dst_y, $qcode_width, $qcode_height);

            //贴第二套模板(紫色上层图)
            $srcImage=imagecreatefrompng(C('CARD_DOMAIN')."/Uploads/CardImgs/default2.png"); 
            //紫色图原图宽度
            $srcImage_width = imagesx($srcImage);
            //紫色图原图高度
            $srcImage_height = imagesy($srcImage);
            //紫色图放置的x坐标
            $x = 0;
            //紫色图放置的y坐标
            $y = 0;
            //紫色图缩放后宽度
            $dst_w = 900;
            //紫色图缩放后高度
            $dst_y = 540;
            imagecopyresampled($im, $srcImage, $x, $y, 0, 0, $dst_w, $dst_y, $srcImage_width, $srcImage_height);

            //贴文字
            $pfont = "./Uploads/Ttf/phone.otf";//电话号码字体
            $wfont = "./Uploads/Ttf/name.TTF";//文字字体
            $white=imagecolorallocate($im,255,255,255);  
            // 文字相对于图片所在的高度
            $mobile_HeightOffset = 80;//电话
            $name_HeightOffset = 342;//姓名
            $company_HeightOffset = 216;//公司名称
            $position_HeightOffset = 450;//职位
            //如果公司名称长度小于16个字，则第一个字的高度居中，否则排第一行
            $company_len = abslength($res['company']);//字符串长度
            if($company_len <=10){
                $company_HeightOffset = 260;//地址
                $fourth = urldecode($res['company']);//公司名称
                $company_word = utf8_substr($fourth,0,1);//地址第一个字
                $width_company_word = imagettfbbox(42, 0, $wfont, $company_word);
                $x_company_word = ceil((900 - $width_company_word[2])/4+300);
                imagettftext($im, 25.5, 0, $x_company_word, $company_HeightOffset, $white, $wfont, $company_word);
                $company_word = utf8_substr($fourth,1,15);//地址第一个字
                $x_company_word = ceil((900 - $width_company_word[2])/4+335);
                imagettftext($im, 25.5, 0, $x_company_word, $company_HeightOffset, $white, $wfont, $company_word);

            }else{
                $company_HeightOffset = 260;//地址
                $fourth = urldecode($res['company']);//公司名称
                $company_word = utf8_substr($fourth,0,1);//地址第一个字
                $width_company_word = imagettfbbox(42, 0, $wfont, $company_word);
                $x_company_word = ceil((900 - $width_company_word[2])/4+300);
                imagettftext($im, 25.5, 0, $x_company_word, $company_HeightOffset, $white, $wfont, $company_word);
                $company_word = utf8_substr($fourth,1,9);//地址第一个字
                $x_company_word = ceil((900 - $width_company_word[2])/4+335);
                imagettftext($im, 25.5, 0, $x_company_word, $company_HeightOffset, $white, $wfont, $company_word);
                //第二行
                $company_word = utf8_substr($fourth,10,1);
                $x_company_word = ceil((900 - $width_company_word[2])/4+300);
                imagettftext($im, 25.5, 0, $x_company_word, $company_HeightOffset+45, $white, $wfont, $company_word);
                $company_word = utf8_substr($fourth,11,9);
                $x_company_word = ceil((900 - $width_company_word[2])/4+335);
                imagettftext($im, 25.5, 0, $x_company_word, $company_HeightOffset+45, $white, $wfont, $company_word);
            } 
            //姓名

            // 解码main.php里img标签已编码的URL字符串，包括first, last以及location
            $first = urldecode($name);//电话
            $second = urldecode($mobile);//姓名
            $third = urldecode($res['company']);//公司名称
            $five = urldecode($res['position']);//职位
            /*
            * 取得需要向图片上添加的文本的范围。imagettfbbox() 返回一个含有8个
            * 单元的数组表示了文本外框的四个角。
            */
            // $len = abslength($content);//字符串长度
            $company_first_word = utf8_substr($third,0,1);//公司名称第一个字
            $company_last_word = utf8_substr($third,1,30);//公司名称除第一个字
            $position_first_word = utf8_substr($five,0,1);//职位第一个字
            $position_last_word = utf8_substr($five,1,10);//职位除第一个字

            $width_first = imagettfbbox(68, 0, $wfont, $first);
            $width_second = imagettfbbox(42, 0, $pfont, $second);
            $width_company_first_word = imagettfbbox(42, 0, $wfont, $company_first_word);//公司第一个字
            $width_position_first_word = imagettfbbox(42, 0, $wfont, $position_first_word);//职位第一个字
            $width_five = imagettfbbox(42, 0, $wfont, $five);//职位

            // 进一法取整, $width_变量[2]代表右下角X坐标位置
            $x_first = ceil((900 - $width_first[2])/5-10);
            $x_second = ceil((900 - $width_second[2])/2+209);
            $x_third = ceil((900 - $width_third[2])/4+450);
            $x_company_first_word = ceil((900 - $width_company_first_word[2])/4+300);
            $x_company_last_word = ceil((900 - $width_company_first_word[2])/4+324);
            $x_position_first_word = ceil((900 - $width_position_first_word[2])/2-284);
            $x_position_last_word = ceil((900 - $width_position_first_word[2])/2-260);
            $x_five = ceil((900 - $width_five[2])/5+30);


            imagettftext($im, 50, 0, $x_first, $name_HeightOffset+58, $white, $wfont, $first);
            imagettftext($im, 33, 0, $x_second, $mobile_HeightOffset+90, $white, $pfont, $second);
            // imagettftext($im, 18, 0, $x_company_first_word, $company_HeightOffset, $white, $wfont, $company_first_word);
            // imagettftext($im, 18, 0, $x_company_last_word, $company_HeightOffset, $white, $wfont, $company_last_word);
            // imagettftext($im, 21, 0, $x_position_first_word, $position_HeightOffset, $white, $wfont, $position_first_word);
            // imagettftext($im, 21, 0, $x_position_last_word, $position_HeightOffset, $white, $wfont, $position_last_word);
            imagettftext($im, 22, 0, $x_five, $position_HeightOffset, $white, $wfont, $five);
        }else{
            //创建画布(默认黑色)
            $im=imagecreatetruecolor($width,$height);
            //修改画布为白色
            $blue=imagecolorallocate($im,255,255,0);  
            imagefill($im,0,0,$blue); 
            // imagecolortransparent($im,$blue);

            //贴头像
            //头像图片类型
            $pos = strrpos($head_img_path,'.');  
            $img_type = substr($head_img_path,$pos+1); 
            if($img_type == 'png' || $img_type == 'PNG'){
                $headImage=imagecreatefrompng($head_img_path);
            }else{
                $headImage=imagecreatefromjpeg($head_img_path);
            }
            //头像原图宽度
            $headImage_width = imagesx($headImage);
            //头像原图高度
            $headImage_height = imagesy($headImage);
            //头像放置的x坐标
            $x = 100;
            //头像放置的y坐标
            $y = 138;
            //头像缩放后宽度
            $dst_w = 264;
            //头像缩放后高度
            $dst_y = 264;
            imagecopyresampled($im, $headImage, $x, $y, 0, 0, $dst_w, $dst_y, $headImage_width, $headImage_height);
        
            //贴二维码
            $qcode=imagecreatefrompng($res['card_code']);
            //二维码原图宽度
            $qcode_width = imagesx($qcode);
            //二维码原图高度
            $qcode_height = imagesy($qcode);
            //二维码放置的x坐标
            $x = 630;
            //二维码放置的y坐标
            $y = 303;
            //二维码缩放后宽度
            $dst_w = 172;
            //二维码缩放后高度
            $dst_y = 172;
            imagecopyresampled($im, $qcode, $x, $y, 0, 0, $dst_w, $dst_y, $qcode_width, $qcode_height);

            //贴紫色上层图
            $srcImage=imagecreatefrompng(C('CARD_DOMAIN')."/Uploads/CardImgs/defalut.png"); 
            //紫色图原图宽度
            $srcImage_width = imagesx($srcImage);
            //紫色图原图高度
            $srcImage_height = imagesy($srcImage);
            //紫色图放置的x坐标
            $x = 0;
            //紫色图放置的y坐标
            $y = 0;
            //紫色图缩放后宽度
            $dst_w = 900;
            //紫色图缩放后高度
            $dst_y = 540;
            imagecopyresampled($im, $srcImage, $x, $y, 0, 0, $dst_w, $dst_y, $srcImage_width, $srcImage_height);

            //贴文字
            $font = "./Uploads/Ttf/simhei.ttf";
            $white=imagecolorallocate($im,255,255,255);  
            // 文字相对于图片所在的高度
            $name_HeightOffset = 107;
            $mobile_HeightOffset = 160;
            // 解码main.php里img标签已编码的URL字符串，包括first, last以及location
            $first = urldecode($name);
            $last = urldecode($mobile);
            /*
            * 取得需要向图片上添加的文本的范围。imagettfbbox() 返回一个含有8个
            * 单元的数组表示了文本外框的四个角。
            */
            $width_first = imagettfbbox(68, 0, $font, $first);
            $width_last = imagettfbbox(42, 0, $font, $last);
             
            // 进一法取整, $width_变量[2]代表右下角X坐标位置
            // $x_first = ceil(900 - $width_first[2]-42);
            $x_first = ceil((900 - $width_first[2])/2+280);
            $x_last = ceil(900 - $width_last[2]-70);
            imagettftext($im, 53, 0, $x_first, $name_HeightOffset+58, $white, $font, $first);
            imagettftext($im, 37, 0, $x_last, $mobile_HeightOffset+90, $white, $font, $last);

        }

        $new_file = './Uploads/CardImgs/'.date('Y-m-d',time())."/";
        if(!file_exists($new_file))
        {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($new_file, 0700);
        }
        // header("content-type:image/png");
        $path = $new_file.$card_id.'.png';
        // imagepng($im);
        imagepng($im,$path);

        successOutputJson('http://'.$_SERVER['HTTP_HOST'].$path);
    }

    /**
     * 选择制作页生成名片图片
     */
    public function ChooseCCardPictrue(){

        $id = I('get.id', 0, 'intval');//名片id

        //图片高度宽度
        $height = 540;
        $width = 900;

        //云名片信息
        $dao = M('User');
        $res = $dao->find($id);
        if (empty($res)) {
            return $this->status403();
        }else{
            if(empty($res['card_code'])){
                //没有名片二维码
                $path = $this->qrurl($card_id);
                if($path === -1){//生成云名片二维码 返回路径
                    errorOutputJson('i'.__LINE__);
                }
                $code_data['id'] = $id;//名片id
                $code_data['card_code'] = $path;
                $rres = D('User')->save($code_data);
                if($rres === false){
                    errorOutputJson('i'.__LINE__);
                }
                $res['card_code'] = 'https://'.$_SERVER['HTTP_HOST'].$path;//二维码路径
            }else{
                $res['card_code'] = 'https://'.$_SERVER['HTTP_HOST'].$res['card_code'];
            }
        }
        
        $name = $res['name'];// 姓名 爱文爱文  艾小雯
        $mobile = $res['moblie_phone'];// 电话

        if(empty($res['head_img'])){
            $head_img_path = C('CARD_DOMAIN')."/Uploads/Qcode/defalut_heade.png";//默认头像路径
        }else{
            $dres = D('Picture')->ImgInfo($res['head_img']);
            $head_img_path = $dres['path'];//头像路径
        }

        //头像图片类型
        $pos = strrpos($head_img_path,'.');  
        $img_type = substr($head_img_path,$pos+1);          

        //创建画布(默认黑色)
        $im=imagecreatetruecolor($width,$height);
        //修改画布为白色
        $white=imagecolorallocate($im,255,255,255);  
        imagefill($im,0,0,$white); 


        //贴头像
        if($img_type == 'png' || $img_type == 'PNG'){
            $headImage=imagecreatefrompng($head_img_path);
        }else{
            $headImage=imagecreatefromjpeg($head_img_path);
        }
        //头像原图宽度
        $headImage_width = imagesx($headImage);
        //头像原图高度
        $headImage_height = imagesy($headImage);
        //头像放置的x坐标
        $x = 100;
        //头像放置的y坐标
        $y = 138;
        //头像缩放后宽度
        $dst_w = 264;
        //头像缩放后高度
        $dst_y = 264;
        imagecopyresampled($im, $headImage, $x, $y, 0, 0, $dst_w, $dst_y, $headImage_width, $headImage_height);
    
        //贴二维码
        $qcode=imagecreatefrompng($res['card_code']);

        //二维码原图宽度
        $qcode_width = imagesx($qcode);
        //二维码原图高度
        $qcode_height = imagesy($qcode);
        //二维码放置的x坐标
        $x = 630;
        //二维码放置的y坐标
        $y = 303;
        //二维码缩放后宽度
        $dst_w = 172;
        //二维码缩放后高度
        $dst_y = 172;
        imagecopyresampled($im, $qcode, $x, $y, 0, 0, $dst_w, $dst_y, $qcode_width, $qcode_height);

        //贴紫色上层图
        $srcImage=imagecreatefrompng(C('CARD_DOMAIN')."/Uploads/CardImgs/defalut.png"); 
        //紫色图原图宽度
        $srcImage_width = imagesx($srcImage);
        //紫色图原图高度
        $srcImage_height = imagesy($srcImage);
        //紫色图放置的x坐标
        $x = 0;
        //紫色图放置的y坐标
        $y = 0;
        //紫色图缩放后宽度
        $dst_w = 900;
        //紫色图缩放后高度
        $dst_y = 540;
        imagecopyresampled($im, $srcImage, $x, $y, 0, 0, $dst_w, $dst_y, $srcImage_width, $srcImage_height);

        //贴文字
        $font = "./Uploads/Ttf/simhei.ttf";
        // 文字相对于图片所在的高度
        $name_HeightOffset = 107;
        $mobile_HeightOffset = 160;
        // 解码main.php里img标签已编码的URL字符串，包括first, last以及location
        $first = urldecode($name);
        $last = urldecode($mobile);
        /*
        * 取得需要向图片上添加的文本的范围。imagettfbbox() 返回一个含有8个
        * 单元的数组表示了文本外框的四个角。
        */
        $width_first = imagettfbbox(68, 0, $font, $first);
        $width_last = imagettfbbox(42, 0, $font, $last);
         
        // 进一法取整, $width_变量[2]代表右下角X坐标位置
        // $x_first = ceil(900 - $width_first[2]-42);
        $x_first = ceil((900 - $width_first[2])/2+280);
        $x_last = ceil(900 - $width_last[2]-70);
        imagettftext($im, 53, 0, $x_first, $name_HeightOffset+58, $white, $font, $first);
        imagettftext($im, 37, 0, $x_last, $mobile_HeightOffset+90, $white, $font, $last);

        header("content-type:image/png");
        imagepng($im);

    }


    /**
     * 点击保存到通讯录
     */
    public function SaveAddrBook() {
        // 9mtnc4p33ibsnhl0ep533jgoi5
        
        if (empty($_GET['id'])) {
            return $this->status403();
        }
        $cart_id = $_GET['id'];

        require_once API_PATH.'Other/qrcode/phpqrcode.php';
        $dao = M('User');
        // $id = intval($_GET['id']);
        $id = $cart_id;
        $info = $dao->find($id);
        if (empty($info)) {
            return $this->status403();
        }
        $vcard = array(
            'vname' => $info['name'],
            'vtel' => $info['telephone'],
            'vemail' => $info['email'],
            'vaddress' => $info['address'],
        );

        // if ($info['head_img']) {
        //     $info['head_img'] = get_cover($info['head_img'], 'path');
        // }
        $chl = "BEGIN:VCARD\nVERSION:3.0" . //vcard头信息
                "\nFN:" . $vcard['vname'] .
                "\nTEL:" . $vcard['vtel'] .
                "\nEMAIL:" . $vcard['vemail'] .
                "\nADR;WORK:" . $vcard['vaddress'];
        if ($info['company']) {
            $chl.="\nORG:" . $info['company'];
        }
        if ($info['position']) {
            $chl.="\nTITLE:" . $info['position'];
        }
        if ($info['department']) {
            $chl.="\nROLE:" . $info['department'];
        }
        if ($info['homepage_url']) {
            $chl.="\nURL:" . $info['homepage_url'];
        }
        if ($info['nickname']) {
            $chl.="\nNICKNAME:" . $info['nickname'];
        }
        if ($info['moblie_phone']) {
            $chl.="\nTEL;CELL;VOICE:" . $info['moblie_phone'];
        }
        // if ($info['head_img']) {
        //     $chl.="\nPHOTO;VALUE=URL;TYPE=GIF:".C('CARD_DOMAIN') . $info['head_img'];
        // }

        $chl.="\nEND:VCARD";
        //vcard尾信息
        $errorCorrectionLevel = 'L';
        //容错级别
        $matrixPointSize = 6;
        //生成图片大小
        //生成二维码图片
        $logo = empty($info['head_img']) ? false : realpath(__ROOT__).$info['head_img'];
        $QR = $logo ? C('CARD_QCODE_PATH').'temp_qrcode.png' : false;
        $QR = false;
        //生成二维码图片
        \QRcode::png($chl, $QR, $errorCorrectionLevel, $matrixPointSize, 2);
        //已经生成的原始二维码图
        if ($logo !== false) {
            $QR = imagecreatefromstring(file_get_contents($QR));
            $logo = imagecreatefromstring(file_get_contents($logo));
            $QR_width = imagesx($QR);
            //二维码图片宽度
            $QR_height = imagesy($QR);
            //二维码图片高度
            $logo_width = imagesx($logo);
            //logo图片宽度
            $logo_height = imagesy($logo);
            //logo图片高度
            $logo_qr_width = $QR_width / 5;
            $scale = $logo_width / $logo_qr_width;
            $logo_qr_height = $logo_height / $scale;
            $from_width = ($QR_width - $logo_qr_width) / 2;
            //重新组合图片并调整大小
            imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
            //输出图片
            Header("Content-type: image/png");
            ImagePng($QR);
        }
    
    }

    protected function status403()
    {
        @header('HTTP/1.1 403 Forbidden'); 
        @header("status: 403 Forbidden"); 
        exit('<html><head><title>403 Forbidden</title></head><body bgcolor="white">403 Forbidden</body></html>');
    }

    /**
     * 生成贺卡图片
     */
    public function CreateBlessCardPictrue(){
        $data = inputJson();

        // 假数据
        // $data['back_path'] = 'http://www.ttc.com/Uploads/Picture/xin.gif';
        // $data['card_temp_path'] = 'http://www.weiweishai.net/Uploads/Picture/2018-02-13/5a82b0708e2ef.png';
        // $data['img_path'] = 'http://www.weiweishai.net./Uploads/Picture/2018-02-14/1518568688.jpeg';
        // $data['qcode_path'] = 'http://www.weiweishai.net./Uploads/Qcode/2018-02-14/108.make.png';
        // $data['word_path'] = '';
        // $data['content'] = '我身边各位情侣欢迎乘坐微微晒情缘航班，本次航班从恋爱机场起飞，途经浪漫站、甜蜜站，将于情人节抵达幸福航空港。情人节快乐';
        // $data['name'] = '邹家杰';
        // $data['phpsid'] = 'hdkjkldks';
        // $data['cid'] = 1;
        // $data['bid'] = 2;
        // $data['phpsid'] = 'otb9a50k6h10r90ks9ffcro20r';
        // $data['word_color'] = '255,255,0';

        
        $data['chancode_path'] = C('CARD_DOMAIN').'/Uploads/CardImgs/chanCode.png';//长按二维码文字的图片路径

        if(empty($data['word_color'])){
            $data['word_color'] = '0,0,0';
        }
        $color_arr = explode( ',',$data['word_color']);
        foreach ($color_arr as $k => $v) {
            if($k == 0){
                $color1 = $v;
            }elseif($k == 1){
                $color2 = $v;
            }elseif($k == 2){
                $color3 = $v;
            }
        }

        //查用户id
        $uires = D('CardMember')->where(['session_id'=>$data['phpsid']])->find();
        if(!empty($uires)){
            $user_id = $uires['id'];
            //生成二维码，并获取路径
            $data['qcode_path'] = C('CARD_DOMAIN').$this->ChooseMakeqrurl($data['cid'],$data['bid'],$user_id);
        }
        
        //图片高度宽度
        $width = 1080;
        $height = 1810;

        $png = ['png','PNG'];
        $jpg = ['jpg','JPG','jpeg','JPEG'];
        $gif = ['gif','GIF'];   

        //背景链接
        $back_path = $data['back_path'];
        //模板链接
        $card_temp_path = $data['card_temp_path'];
        //中间图链接
        $img_path = $data['img_path'];
        //文字模板图链接
        $word_path = $data['word_path'];
        //二维码链接
        $qcode_path = $data['qcode_path'];

        //自定义文字
        $data['content'] = str_replace(array("\r\n", "\r", "\n"), " ", $data['content']);  
        $content = $data['content'];

        //创建画布(默认黑色)
        $im=imagecreatetruecolor($width,$height);
        //修改画布为白色
        $white=imagecolorallocate($im,255,255,255);  
        imagefill($im,0,0,$white); 

        //贴背景
        $pos = strrpos($back_path,'.');  
        $img_type = substr($back_path,$pos+1);
        if(in_array($img_type,$png)){
            $backImage=imagecreatefrompng($back_path);
        }elseif(in_array($img_type,$jpg)){
            $backImage=imagecreatefromjpeg($back_path);
        }elseif(in_array($img_type,$gif)){
            $backImage=imagecreatefromgif($back_path);
        }
        //头像原图宽度
        $backImage_width = imagesx($backImage);
        //头像原图高度
        $backImage_height = imagesy($backImage);
        //头像放置的x坐标
        $x = 0;
        //头像放置的y坐标
        $y = 0;
        //头像缩放后宽度
        $dst_w = 1080;
        //头像缩放后高度
        $dst_y = 1810;
        imagecopyresampled($im, $backImage, $x, $y, 0, 0, $dst_w, $dst_y, $backImage_width, $backImage_height);

        //贴中间图
        $pos = strrpos($img_path,'.');  
        $img_type = substr($img_path,$pos+1);
        if(in_array($img_type,$png)){
            $middeleImage=imagecreatefrompng($img_path);
        }elseif(in_array($img_type,$jpg)){
            $middeleImage=imagecreatefromjpeg($img_path);
        }elseif(in_array($img_type,$gif)){
            $middeleImage=imagecreatefromgif($img_path);
        }
        //中间图原图宽度
        $card_Image_width = imagesx($middeleImage);
        //中间图原图高度
        $card_Image_height = imagesy($middeleImage);
        //中间图放置的x坐标
        $x =204;
        //中间图放置的y坐标
        $y = 266;
        //中间图缩放后宽度
        $dst_w = 677;
        //中间图缩放后高度
        $dst_y = 970;
        imagecopyresampled($im, $middeleImage, $x, $y, 0, 0, $dst_w, $dst_y, $card_Image_width, $card_Image_height);

        //贴模板
        $pos = strrpos($card_temp_path,'.');  
        $img_type = substr($card_temp_path,$pos+1);
        if(in_array($img_type,$png)){
            $card_temp_Image=imagecreatefrompng($card_temp_path);
        }elseif(in_array($img_type,$jpg)){
            $card_temp_Image=imagecreatefromjpeg($card_temp_path);
        }elseif(in_array($img_type,$gif)){
            $card_temp_Image=imagecreatefromgif($card_temp_path);
        }
        //头像原图宽度
        $card_temp_Image_width = imagesx($card_temp_Image);
        //头像原图高度
        $card_temp_Image_height = imagesy($card_temp_Image);
        //头像放置的x坐标
        $x = 0;
        //头像放置的y坐标
        $y = 0;
        //头像缩放后宽度
        $dst_w = 1080;
        //头像缩放后高度
        $dst_y = 1810;
        imagecopyresampled($im, $card_temp_Image, $x, $y, 0, 0, $dst_w, $dst_y, $card_temp_Image_width, $card_temp_Image_height);

        //贴文字模板
        $font = "./Uploads/Ttf/simhei.ttf";
        if(!empty($word_path)){
            $pos = strrpos($word_path,'.');  
            $img_type = substr($word_path,$pos+1);
            if(in_array($img_type,$png)){
                $word_Image=imagecreatefrompng($word_path);
            }elseif(in_array($img_type,$jpg)){
                $word_Image=imagecreatefromjpeg($word_path);
            }elseif(in_array($img_type,$gif)){
                $word_Image=imagecreatefromgif($word_path);
            }
            //文字模板原图宽度
            $word_Image_width = imagesx($word_Image);
            //文字模板原图高度
            $word_Image_height = imagesy($word_Image);
            //文字模板放置的x坐标
            $x = 50;
            //文字模板放置的y坐标
            $y = 1380;
            //文字模板缩放后宽度
            $dst_w = 616;
            //文字模板缩放后高度
            $dst_y = 280;
            imagecopyresampled($im, $word_Image, $x, $y, 0, 0, $dst_w, $dst_y, $word_Image_width, $word_Image_height);
        }else{
            //自定义文字
            $len = abslength($content);//字符串长度
            $font = "./Uploads/Ttf/simhei.ttf";
            // 文字相对于图片所在的高度
            $HeightOffset = 360;
            $ImgSize = 1080;

            $black=imagecolorallocate($im,0,0,0);  
            //自定义字体颜色
            $yellow=imagecolorallocate($im,$color1,$color2,$color3);  

            if($len <= 15){//放一行
                // 解码main.php里img标签已编码的URL字符串，包括first, last以及location
                $first = urldecode($content);
                /*
                * 取得需要向图片上添加的文本的范围。imagettfbbox() 返回一个含有8个
                * 单元的数组表示了文本外框的四个角。
                */
                // $ImgSize = getimagesize($back_path);//图片宽度
                $width_first = imagettfbbox(30, 0, $font, $first);
                $x_first = ceil(($ImgSize - $width_first[2]) / 2) - 166;
                // 进一法取整, $width_变量[2]代表右下角X坐标位置
                imagettftext($im, 30, 0, $x_first, $HeightOffset+1080, $yellow, $font, $first);
            }elseif ($len <= 30) {
                //第一行
                $subContent = utf8_substr($content,0,15);
                 // 解码main.php里img标签已编码的URL字符串，包括first, last以及location
                $first = urldecode($subContent);
                $width_first = imagettfbbox(30, 0, $font, $first);
                $x_first = ceil(($ImgSize - $width_first[2]) / 2) - 166;
                // 进一法取整, $width_变量[2]代表右下角X坐标位置
                imagettftext($im, 30, 0, $x_first, $HeightOffset+1080, $yellow, $font, $first);
                //第二行
                $subContent = utf8_substr($content,15,15);
                // 解码main.php里img标签已编码的URL字符串，包括first, last以及location
                $first = urldecode($subContent);
                $width_first = imagettfbbox(30, 0, $font, $first);
                $x_first = ceil(($ImgSize - $width_first[2]) / 2) - 166;
                // 进一法取整, $width_变量[2]代表右下角X坐标位置
                imagettftext($im, 30, 0, $x_first, $HeightOffset+1140, $yellow, $font, $first);
            }elseif($len <= 45){
                //第一行
                $subContent = utf8_substr($content,0,15);
                 // 解码main.php里img标签已编码的URL字符串，包括first, last以及location
                $first = urldecode($subContent);
                $width_first = imagettfbbox(30, 0, $font, $first);
                $x_first = ceil(($ImgSize - $width_first[2]) / 2) - 166;
                // 进一法取整, $width_变量[2]代表右下角X坐标位置
                imagettftext($im, 30, 0, $x_first, $HeightOffset+1080, $yellow, $font, $first);
                //第二行
                $subContent = utf8_substr($content,15,15);
                // 解码main.php里img标签已编码的URL字符串，包括first, last以及location
                $first = urldecode($subContent);
                $width_first = imagettfbbox(30, 0, $font, $first);
                $x_first = ceil(($ImgSize - $width_first[2]) / 2) - 166;
                // 进一法取整, $width_变量[2]代表右下角X坐标位置
                imagettftext($im, 30, 0, $x_first, $HeightOffset+1140, $yellow, $font, $first);
                //第三行
                $subContent = utf8_substr($content,30,15);
                // 解码main.php里img标签已编码的URL字符串，包括first, last以及location
                $first = urldecode($subContent);
                $width_first = imagettfbbox(30, 0, $font, $first);
                $x_first = ceil(($ImgSize - $width_first[2]) / 2) - 166;
                // 进一法取整, $width_变量[2]代表右下角X坐标位置
                imagettftext($im, 30, 0, $x_first, $HeightOffset+1200, $yellow, $font, $first);
            }else{
                //第一行
                $subContent = utf8_substr($content,0,15);
                 // 解码main.php里img标签已编码的URL字符串，包括first, last以及location
                $first = urldecode($subContent);
                $width_first = imagettfbbox(30, 0, $font, $first);
                $x_first = ceil(($ImgSize - $width_first[2]) / 2) - 166;
                // 进一法取整, $width_变量[2]代表右下角X坐标位置
                imagettftext($im, 30, 0, $x_first, $HeightOffset+1080, $yellow, $font, $first);
                //第二行
                $subContent = utf8_substr($content,15,15);
                // 解码main.php里img标签已编码的URL字符串，包括first, last以及location
                $first = urldecode($subContent);
                $width_first = imagettfbbox(30, 0, $font, $first);
                $x_first = ceil(($ImgSize - $width_first[2]) / 2) - 166;
                // 进一法取整, $width_变量[2]代表右下角X坐标位置
                imagettftext($im, 30, 0, $x_first, $HeightOffset+1140, $yellow, $font, $first);
                //第三行
                $subContent = utf8_substr($content,30,15);
                // 解码main.php里img标签已编码的URL字符串，包括first, last以及location
                $first = urldecode($subContent);
                $width_first = imagettfbbox(30, 0, $font, $first);
                $x_first = ceil(($ImgSize - $width_first[2]) / 2) - 166;
                // 进一法取整, $width_变量[2]代表右下角X坐标位置
                imagettftext($im, 30, 0, $x_first, $HeightOffset+1200, $yellow, $font, $first);
                //第四行
                $subContent = utf8_substr($content,45,15);
                // 解码main.php里img标签已编码的URL字符串，包括first, last以及location
                $first = urldecode($subContent);
                $width_first = imagettfbbox(30, 0, $font, $first);
                $x_first = ceil(($ImgSize - $width_first[2]) / 2) - 166;
                // 进一法取整, $width_变量[2]代表右下角X坐标位置
                imagettftext($im, 30, 0, $x_first, $HeightOffset+1260, $yellow, $font, $first);
            }

        }

        //贴二维码
        $pos = strrpos($qcode_path,'.');  
        $img_type = substr($qcode_path,$pos+1);          
        if($img_type == 'png' || $img_type == 'PNG'){
            $qcode_Image=imagecreatefrompng($qcode_path);
        }else{
            $qcode_Image=imagecreatefromjpeg($qcode_path);
        }
        //二维码原图宽度
        $qcode_Image_width = imagesx($qcode_Image);
        //二维码原图高度
        $qcode_Image_height = imagesy($qcode_Image);
        //二维码放置的x坐标
        $x = 760;
        //二维码放置的y坐标
        $y = 1390;
        //二维码缩放后宽度
        $dst_w = 240;
        //二维码缩放后高度
        $dst_y = 240;
        imagecopyresampled($im, $qcode_Image, $x, $y, 0, 0, $dst_w, $dst_y, $qcode_Image_width, $qcode_Image_height);


         //来自谁的祝福
        // if(empty($data['name'])){
        //     $data['name'] = '好友';
        // }
        //通过session_id查openid
        $openid = D('CardMember')->getMemberInfo(array('session_id'=>$data['phpsid']));
        if(!empty($openid['name'])){
            $data['name'] = $openid['name'];
        }elseif(!empty($openid['wechat'])){
            $result = mall_json_post_http_curl('act=wx_login&op=getWXLoginInfo', ['openid' => $openid['wechat']]);
            $data['name'] = $result['data']['nickname'];
        }else{
            $data['name'] = '好友';
        }

        $from_font = './Uploads/Ttf/STXINGKA.ttf';
        $from = '来自';
        $HeightOffset = 1316;
        $ImgSize = 1080;
        $first = urldecode($from);
        $width_first = imagettfbbox(30, 0, $from_font, $first);
        $x_first = ceil(($ImgSize - $width_first[2]) / 2) - 430;
        imagettftext($im, 30, 0, $x_first, $HeightOffset+60, $yellow, $from_font, $first);
        $content = $data['name'].'的祝福:';
        $first = urldecode($content);
        $x_first = ceil(($ImgSize - $width_first[2]) / 2) - 350;
        imagettftext($im, 30, 0, $x_first, $HeightOffset+60, $yellow, $from_font, $first);

        // $content = '长按识别二维码';
        // // 文字相对于图片所在的高度
        // $HeightOffset = 1580;
        // $ImgSize = 1080;
        // $first = urldecode($content);
        // $width_first = imagettfbbox(28, 0, $font, $first);
        // $x_first = ceil(($ImgSize - $width_first[2]) / 2) + 326;
        // $yellow=imagecolorallocate($im,255,255,255);  
        // imagettftext($im, 28, 0, $x_first, $HeightOffset+58, $yellow, $font, $first);

        // $content = '制作贺卡';
        // // 文字相对于图片所在的高度
        // $HeightOffset = 1616;
        // $ImgSize = 1080;
        // $first = urldecode($content);
        // $width_first = imagettfbbox(28, 0, $font, $first);
        // $x_first = ceil(($ImgSize - $width_first[2]) / 2) + 326;
        // $yellow=imagecolorallocate($im,255,255,255);  
        // imagettftext($im, 28, 0, $x_first, $HeightOffset+58, $yellow, $font, $first);

        //长按识别二维码
        $chancode_path = $data['chancode_path'];        
        $chancode_Image=imagecreatefrompng($chancode_path);
        //图片原图宽度
        $chancode_Image_width = imagesx($chancode_Image);
        //图片原图高度
        $chancode_Image_height = imagesy($chancode_Image);
        //图片放置的x坐标
        $x = 758;
        //图片放置的y坐标
        $y = 1630;
        //图片缩放后宽度
        $dst_w = 240;
        //图片缩放后高度
        $dst_y = 105;
        imagecopyresampled($im, $chancode_Image, $x, $y, 0, 0, $dst_w, $dst_y, $chancode_Image_width, $chancode_Image_height);

        // header("content-type:image/png");
        // imagepng($im);

        $new_file = './Uploads/CardImgs/'.date('Y-m-d',time())."/";
        if(!file_exists($new_file))
        {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($new_file, 0700);
        }

        // header("content-type:image/png");
        $path = $new_file.time().'.png';
        imagepng($im,$path);
        successOutputJson('http://'.$_SERVER['HTTP_HOST'].$path);
        
    }


}
